package com.zhu.salesmanagement.network;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;

import com.zhu.salesmanagement.SalesApplication;
import com.zhu.salesmanagement.common.Actions;
import com.zhu.salesmanagement.models.CRCinfoItem;
import com.zhu.salesmanagement.models.CustomerItem;
import com.zhu.salesmanagement.models.CustomerShelfsItem;
import com.zhu.salesmanagement.models.IntegrationStandardItem;
import com.zhu.salesmanagement.models.ProductCategoryItem;
import com.zhu.salesmanagement.models.PusherStoreItem;
import com.zhu.salesmanagement.models.RemindMsgItem;
import com.zhu.salesmanagement.models.SyncProductItem;
import com.zhu.salesmanagement.models.SyncTaskItem;
import com.zhu.salesmanagement.provider.SalesSettings;
import com.zhu.salesmanagement.utils.Util;
import com.zhu.salesmanagement.views.SyncView;

/**
 * 同步
 */
public class SyncData {

    private static final int PUTPHONEMAXCOUNT = 20;
    private static final int SYNC_PUTPHONEMAXCOUNT = 30;

	private static final int SETUP_GETPC = 1; //获取所有产品大类信息
	private static final int SETUP_GETPRODUCT = 2; //获取所有产品信息
	private static final int SETUP_GETREMIND = 3; //用户提醒任务信息
	private static final int SETUP_GETTASK = 4; //获取业务主管每天计划拜访任务信息
	private static final int SETUP_GETCUSTOMER = 5; //基于查访清单，获取对应门店的基础信息
	private static final int SETUP_GETCSHELF = 6; //基于查访清单，获取对应门店的产品大类信息以及门店货架信息
	private static final int SETUP_GETISTANDARD = 7; //获取对应大区综合考评信息
	private static final int SETUP_GETCRC = 8; //基于查访清单，获取对应门店最近一天的CRC资料
	private static final int SETUP_GETCRCOTHER = 9; //基于查访清单，获取对应门店最近一次的重要讯息
	private static final int SETUP_GETSCMSG = 10;  //基于查访清单，获取对应门店最近一次SC的重要讯息
	private static final int SETUP_GETPUSHERSTORE = 11; //基于查访清单，获取对应门店的导购信息
	private static final int STTUP_PUTDTASKACTUALSC = 12; //业务主管上传实际拜访任务信息
	private static final int STTUP_PUTDTASKACTUALSHLEFSC = 13; //业务主管上传门店货架信息
	private static final int SETUP_PUTDTASKACTUALEXHIBITION = 14; //业务主管上传实际陈列考评信息request
	private static final int SETUP_PUTDTASKACTUALINTEGRATION = 15; //业务主管上传实际综合考评request
	private static final int SETUP_PUTDTASKACTUALIMPORTMSG = 16; //业务主管上传重要信息
	private static final int SETUP_PUTDTASKACTUALPHOTO = 17; //上传实际拜访照片 
	private static final int SETUP_PUTNEWADDEDSTORES = 18; //上传新增门店信息
	private static final int SETUP_PUTSTOREPROPERTY = 19; //上传门店特性调查信息
	private static final int SETUP_PUTCOMPELE = 20; //同步完成

	private ArrayList<ProductCategoryItem> mPClist = null;
    private ArrayList<SyncProductItem> mPlist = null;
    private ArrayList<IntegrationStandardItem> mISlist = null;
    private ArrayList<CustomerItem> mCustomerList = null;
    private ArrayList<CRCinfoItem> mCRClist = null;
    private ArrayList<PusherStoreItem> mPusherList = null;
    private ArrayList<CustomerShelfsItem> mCSlist = null;
    private ArrayList<SyncTaskItem> mTaskList = null;
    private ArrayList<RemindMsgItem> mRemindList = null;
    private List<Object> mStorePropertyList = new ArrayList<Object>();
    private List<Object> mStorePhoteList = null;

    private Context mContext = null;
    private int mConnectStep = -1;
    private int mPutCount = 0;
    private int mTotalCount = 0;
    private Handler mHandler = null;
    private int retryCount = 0;
    private CustomerShelfData mCustomerShelfData = null;
    private long mNetworkTime = 0;

    /**
     * 构造函数
     * @param context 上下文
     * @param handler 结果
     */
    public SyncData(Context context, Handler handler){
    	mContext = context;
    	mHandler = handler;
    }

    /**
     * 开始同步
     */
    public void startSync(){
    	if(mConnectStep == -1){
    		resetData();
    		updateisFirst();
    		if(mCustomerShelfData == null){
    			mCustomerShelfData = new CustomerShelfData(mContext, Util.mVerifyUserResult.id, Util.mVerifyUserResult.mUUID);
    		}
    		mCustomerShelfData.parseData();
    		mStorePhoteList = PostData.getStorePhotos(Util.mVerifyUserResult.id, Util.mVerifyUserResult.mUUID, mContext);
    		mPutCount = 0;
    		mTotalCount = 0;
    		if(mStorePhoteList.size() > SYNC_PUTPHONEMAXCOUNT){
    		    mHandler.sendEmptyMessage(SyncView.SYNC_IMAGEFAILEDEND);
    		    return;
    		}
    		requestNetworkTime();
    	}
    }

    /**
     * 重置数据
     */
    private void resetData(){
    	if(mPClist != null){
    		mPClist.clear();
    		mPClist = null;
    	}
    	if(mPlist != null){
    		mPlist.clear();
    		mPlist = null;
    	}
    	if(mISlist != null){
    	    mISlist.clear();
    	    mISlist = null;
    	}
    	if(mCustomerList != null){
    		mCustomerList.clear();
    		mCustomerList = null;
    	}
    	if(mCSlist != null){
    		mCSlist.clear();
    		mCSlist = null;
    	}
    	if(mTaskList != null){
            mTaskList.clear();
            mTaskList = null;
        }
        if(mRemindList != null){
            mRemindList.clear();
            mRemindList = null;
        }
        if(mCRClist != null){
            mCRClist.clear();
            mCRClist = null;
        }
        if(mPusherList != null){
            mPusherList.clear();
            mPusherList = null;
        }
    	if(mStorePropertyList != null){
    	    mStorePropertyList.clear();
    	}
    	if(mStorePhoteList != null){
    	    mStorePhoteList.clear();
    	    mStorePhoteList = null;
    	}
    	ParseData.mVisitedCustomersList.clear();
    }

    /**
     * 重试连接
     */
    private void retryConnect(){
    	switch(mConnectStep){
    		case SETUP_GETPC:
    			getProductCategory();
    			break;
    		case SETUP_GETPRODUCT:
    			getProduct();
    			break;
            case SETUP_GETREMIND:
                getRemindMSG();
                break;
            case SETUP_GETTASK:
                getMyTaskList();
                break;
    		case SETUP_GETCUSTOMER:
    			getCustomer();
    			break;
    		case SETUP_GETCSHELF:
    			getProductCategoryAndCustomerShelfOfCustomers();
    			break;
            case SETUP_GETISTANDARD:
                getIntegrationStandard();
                break;
    		case SETUP_GETCRC:
    		    getCRC();
    		    break;
    		case SETUP_GETCRCOTHER:
    		    getCRCOther();
    			break;
    		case SETUP_GETSCMSG:
    		    getSCMsg();
    		    break;
    		case SETUP_GETPUSHERSTORE:
    		    getPusherStore();
    			break;
    		case STTUP_PUTDTASKACTUALSC:
    		    putDatingTaskActualSC();
    		    break;
    		case STTUP_PUTDTASKACTUALSHLEFSC:
    		    putCustomerShelf();
    		    break;
    		case SETUP_PUTDTASKACTUALEXHIBITION:
    		    putDatingTaskActualExhibition();
    			break;
    		case SETUP_PUTDTASKACTUALINTEGRATION:
    			putDatingTaskActualIntegration();
    			break;
    		case SETUP_PUTDTASKACTUALIMPORTMSG:
    		    putDatingTaskActualImportMsg();
    		    break;
    		case SETUP_PUTDTASKACTUALPHOTO:
    		    putDatingTaskActualPhoto();
    		    break;
    		case SETUP_PUTNEWADDEDSTORES:
    			putCustomerTemp();
    			break;
    		case SETUP_PUTSTOREPROPERTY:
    		    putStoreProrery();
    		    break;
    		case SETUP_PUTCOMPELE:
    			putCompleted();
    			break;
    	}
    }

    /**
     * 是否第一次同步
     */
    private void updateisFirst(){
    	Cursor cursor = null; 
		try {
			cursor = mContext.getContentResolver().query(SalesSettings.CustomerSColumns.CONTENT_URI_NO_NOTIFICATION, SyncUtils.PROJECTION,
					null, null, null);
			if (cursor != null && cursor.getCount() > 0) {
				Util.isFirst = false;
			}else{
				Util.isFirst = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Util.isFirst = true;
		} finally {
			if (cursor != null) {
				cursor.close();
				cursor = null;
			}
		}
    }

    /**
     * getProductCategory
     */
    private void getProductCategory(){
    	mConnectStep = SETUP_GETPC;
    	Log.e("zhu",Util.mVerifyUserResult.id +","+ Util.mVerifyUserResult.mUUID);
    	mHandler.sendEmptyMessage(SyncView.SYNC_1);
		PostMethod.requesProductCategory(Util.mVerifyUserResult.id,
				Util.mVerifyUserResult.mUUID, true, new XMLRPCMethodCallback() {
					public void callFinished(int status, Object result) {
						mPClist = ParseData.parseProductCategory(result, mContext);
						if(mPClist != null){
							retryCount = 0;
							getProduct();
						}else{
							syncFail();
						}
					}
				}, SalesApplication.mApplicaton.getXMLRPCClient());
    }

    /**
     * getProduct
     */
    private void getProduct(){
    	mConnectStep = SETUP_GETPRODUCT;
		PostMethod.requesProducts(Util.mVerifyUserResult.id,
				Util.mVerifyUserResult.mUUID, true, new XMLRPCMethodCallback() {
					public void callFinished(int status, Object result) {
						mPlist = ParseData.parseProducts(result, mContext);
						if(mPlist != null){
							mHandler.sendEmptyMessage(SyncView.SYNC_2);
							retryCount = 0;
							getRemindMSG();
						}else{
							syncFail();
						}
					}
				}, SalesApplication.mApplicaton.getXMLRPCClient());
    }

    /**
     * getRemindMSG
     */
    private void getRemindMSG(){
        mConnectStep = SETUP_GETREMIND;
        PostMethod.requesRemindMsg(Util.mVerifyUserResult.id,
                Util.mVerifyUserResult.mUUID, new XMLRPCMethodCallback() {
                    public void callFinished(int status, Object result) {
                        mRemindList = ParseData.parseRemindMsg(result, mContext);
                        if(mRemindList != null){
                            retryCount = 0;
                            mHandler.sendEmptyMessage(SyncView.SYNC_3);
                            getMyTaskList();
                        }else{
                            syncFail();
                        }
                    }
                }, SalesApplication.mApplicaton.getXMLRPCClient());
    }

    /**
     * 获取清单拜访任务
     */
    private void getMyTaskList(){
        mConnectStep = SETUP_GETTASK;
        ParseData.mVisitedCustomersList.clear();
        PostMethod.requesMyTaskList(Util.mVerifyUserResult.id,
                Util.mVerifyUserResult.mUUID, new XMLRPCMethodCallback() {
                    public void callFinished(int status, Object result) {
                        mTaskList = ParseData.parseMyTaskList(result, mContext);
                        if(mTaskList != null){
                            retryCount = 0;
                            mHandler.sendEmptyMessage(SyncView.SYNC_4);
                            getCustomer();
                        }else{
                            syncFail();
                        }
                    }
                }, SalesApplication.mApplicaton.getXMLRPCClient());
    }

    /**
     * getCustomer
     */
    private void getCustomer(){
    	mConnectStep = SETUP_GETCUSTOMER;
    	if(ParseData.mVisitedCustomersList.size() == 0){
    	    getProductCategoryAndCustomerShelfOfCustomers();
    	    return;
    	}
		PostMethod.requesCustomer(Util.mVerifyUserResult.id,
				Util.mVerifyUserResult.mUUID, ParseData.mVisitedCustomersList, new XMLRPCMethodCallback() {
					public void callFinished(int status, Object result) {
						mCustomerList = ParseData.parseCustomer(result, mContext);
						if(mCustomerList != null){
							retryCount = 0;
							getProductCategoryAndCustomerShelfOfCustomers();
						}else{
							syncFail();
						}
					}
				}, SalesApplication.mApplicaton.getXMLRPCClient());
    }

    /**
     * getProductCategoryAndCustomerShelfOfCustomers
     */
    private void getProductCategoryAndCustomerShelfOfCustomers(){
        mConnectStep = SETUP_GETCSHELF;
        if(ParseData.mVisitedCustomersList.size() == 0){
            mHandler.sendEmptyMessage(SyncView.SYNC_5);
            getIntegrationStandard();
            return;
        }
        PostMethod.requesCustomerShelfs(Util.mVerifyUserResult.id,
                    Util.mVerifyUserResult.mUUID, ParseData.mVisitedCustomersList,
                new XMLRPCMethodCallback() {
                    public void callFinished(int status, Object result) {
                        mCSlist = ParseData.parseCustomerShelfs(result, mContext);
                        if (mCSlist != null) {
                            retryCount = 0;
                            mHandler.sendEmptyMessage(SyncView.SYNC_5);
                            getIntegrationStandard();
                        } else {
                            syncFail();
                        }
                    }
                }, SalesApplication.mApplicaton.getXMLRPCClient());
    }

    /**
     * getIntegrationStandard
     */
    private void getIntegrationStandard(){
        mConnectStep = SETUP_GETISTANDARD;
        if(ParseData.mVisitedCustomersList.size() == 0){
            getCRC();
            return;
        }
        PostMethod.requestIntegrationStandard(Util.mVerifyUserResult.id,
                Util.mVerifyUserResult.mUUID, ParseData.mVisitedCustomersList, new XMLRPCMethodCallback() {
                    public void callFinished(int status, Object result) {
                        mISlist = ParseData.parseStandards(result, mContext);
                        if(mISlist != null){
                            retryCount = 0;
                            getCRC();
                        }else{
                            syncFail();
                        }
                    }
                }, SalesApplication.mApplicaton.getXMLRPCClient());
    }

    /**
     * getCRC
     */
    private void getCRC(){
        mConnectStep = SETUP_GETCRC;
        if(ParseData.mVisitedCustomersList.size() == 0){
            getCRCOther();
            return;
        }
        PostMethod.requesCRC(Util.mVerifyUserResult.id,
                    Util.mVerifyUserResult.mUUID, ParseData.mVisitedCustomersList, new XMLRPCMethodCallback() {
                        public void callFinished(int status, Object result) {
                            mCRClist = ParseData.parseCRC(result, mContext);
                            if (mCRClist != null) {
                                retryCount = 0;
                                getCRCOther();
                            } else {
                                syncFail();
                            }
                        }
                    }, SalesApplication.mApplicaton.getXMLRPCClient());
    }

    /**
     * getCRCOther
     */
    private void getCRCOther(){
        mConnectStep = SETUP_GETCRCOTHER;
        if(ParseData.mVisitedCustomersList.size() == 0){
            getSCMsg();
            return;
        }
        PostMethod.requesCRCOther(Util.mVerifyUserResult.id,
                    Util.mVerifyUserResult.mUUID, ParseData.mVisitedCustomersList, new XMLRPCMethodCallback() {
                        public void callFinished(int status, Object result) {
                            boolean isSuccess = ParseData.parseCRCOther(result, mContext, mCRClist);
                            if (isSuccess) {
                                retryCount = 0;
                                getSCMsg();
                            } else {
                                syncFail();
                            }
                        }
                    }, SalesApplication.mApplicaton.getXMLRPCClient());
    }

    /**
     * getSCMsg
     */
    private void getSCMsg(){
        mConnectStep = SETUP_GETSCMSG;
        if(ParseData.mVisitedCustomersList.size() == 0){
            getPusherStore();
            return;
        }
        PostMethod.requesSCMsg(Util.mVerifyUserResult.id,
                    Util.mVerifyUserResult.mUUID, ParseData.mVisitedCustomersList, new XMLRPCMethodCallback() {
                        public void callFinished(int status, Object result) {
                            boolean success = ParseData.parseSCMsg(result, mContext, mCustomerList);
                            if (success) {
                                retryCount = 0;
                                getPusherStore();
                            } else {
                                syncFail();
                            }
                        }
                    }, SalesApplication.mApplicaton.getXMLRPCClient());
    }

    /**
     * getPusherStore
     */
    private void getPusherStore(){
        mConnectStep = SETUP_GETPUSHERSTORE;
        if(ParseData.mVisitedCustomersList.size() == 0){
            mHandler.sendEmptyMessage(SyncView.SYNC_6);
            putDatingTaskActualSC();
            return;
        }
        PostMethod.requesPusherStore(Util.mVerifyUserResult.id,
                    Util.mVerifyUserResult.mUUID, ParseData.mVisitedCustomersList, new XMLRPCMethodCallback() {
                        public void callFinished(int status, Object result) {
                        	mPusherList = ParseData.parsePusherStore(result, mContext);
                            if (mPusherList != null) {
                                retryCount = 0;
                                mHandler.sendEmptyMessage(SyncView.SYNC_6);
                                putDatingTaskActualSC();
                            } else {
                                syncFail();
                            }
                        }
                    }, SalesApplication.mApplicaton.getXMLRPCClient());
    }

    /**
     * putDatingTaskActualSC
     */
    private void putDatingTaskActualSC() {
        mConnectStep = STTUP_PUTDTASKACTUALSC;
        Object[] object = mCustomerShelfData.getVisitedTaskObject();
        if (Actions.DEBUG) {
            Log.e("zhu", "putDatingTaskActualSC=" + object);
        }
        if (object == null) {
            putCustomerShelf();
            return;
        }
        PostMethod.postDatingTaskActual(object, new XMLRPCMethodCallback() {
            public void callFinished(int status, Object result) {
                boolean isSuccess = ParseData.parsePostData(result);
                if (isSuccess) {
                    retryCount = 0;
                    putCustomerShelf();
                } else {
                    syncFail();
                }
            }
        }, SalesApplication.mApplicaton.getXMLRPCClient());
    }

    /**
     * putCustomerShelf
     */
    private void putCustomerShelf(){
        mConnectStep = STTUP_PUTDTASKACTUALSHLEFSC;
        Object[] object = mCustomerShelfData.getShelfObject();
        if(Actions.DEBUG){
            Log.e("zhu","putCustomerShelf="+object);
        }
        if(object == null){
            putDatingTaskActualExhibition();
            return;
        }
        PostMethod.postCustomerShelf(object, new XMLRPCMethodCallback() {
                    public void callFinished(int status, Object result) {
                        boolean isSuccess = ParseData.parsePostData(result);
                        if(isSuccess){
                            retryCount = 0;
                            putDatingTaskActualExhibition();
                        }else{
                            syncFail();
                        }
                    }
                }, SalesApplication.mApplicaton.getXMLRPCClient());
    }

    /**
     * putDatingTaskActualExhibition
     */
    private void putDatingTaskActualExhibition(){
        mConnectStep = SETUP_PUTDTASKACTUALEXHIBITION;
        Object[] object = mCustomerShelfData.getCRCObject();
        if(Actions.DEBUG){
            Log.e("zhu","putDatingTaskActualExhibition="+object);
        }
        if(object == null){
            putDatingTaskActualIntegration();
            return;
        }
        PostMethod.putDatingTaskActualExhibition(object, new XMLRPCMethodCallback() {
                    public void callFinished(int status, Object result) {
                        boolean isSuccess = ParseData.parsePostData(result);
                        if(isSuccess){
                            retryCount = 0;
                            putDatingTaskActualIntegration();
                        }else{
                            syncFail();
                        }
                    }
                }, SalesApplication.mApplicaton.getXMLRPCClient());
    }

    /**
     * putDatingTaskActualIntegration
     */
    private void putDatingTaskActualIntegration(){
        mConnectStep = SETUP_PUTDTASKACTUALINTEGRATION;
        Object[] object = mCustomerShelfData.getCheckResultObject();
        if(Actions.DEBUG){
            Log.e("zhu","putDatingTaskActualIntegration="+object);
        }
        if(object == null){
            putDatingTaskActualImportMsg();
            return;
        }
        PostMethod.putDatingTaskActualIntegration(object, new XMLRPCMethodCallback() {
                    public void callFinished(int status, Object result) {
                        boolean isSuccess = ParseData.parsePostData(result);
                        if(isSuccess){
                            retryCount = 0;
                            putDatingTaskActualImportMsg();
                        }else{
                            syncFail();
                        }
                    }
                }, SalesApplication.mApplicaton.getXMLRPCClient());
    }

    /**
     * putDatingTaskActualImportMsg
     */
    private void putDatingTaskActualImportMsg(){
        mConnectStep = SETUP_PUTDTASKACTUALIMPORTMSG;
        Object[] object = mCustomerShelfData.getCheckImportMsgObject();
        if(Actions.DEBUG){
            Log.e("zhu","putDatingTaskActualImportMsg="+object);
        }
        if(object == null){
            putDatingTaskActualPhoto();
            return;
        }
        PostMethod.putDatingTaskActualImportMsg(object, new XMLRPCMethodCallback() {
                    public void callFinished(int status, Object result) {
                        boolean isSuccess = ParseData.parsePostData(result);
                        if(isSuccess){
                            retryCount = 0;
                            putDatingTaskActualPhoto();
                        }else{
                            syncFail();
                        }
                    }
                }, SalesApplication.mApplicaton.getXMLRPCClient());
    }

    /**
     * putDatingTaskActualPhoto
     */
    private void putDatingTaskActualPhoto(){
        mConnectStep = SETUP_PUTDTASKACTUALPHOTO;
        Object[] object = null;
        if(mStorePhoteList != null && mStorePhoteList.size() > 0){
            mTotalCount = mStorePhoteList.size() / PUTPHONEMAXCOUNT;
            if(mStorePhoteList.size() % PUTPHONEMAXCOUNT != 0){
                mTotalCount++;
            }
            if(mStorePhoteList.size() > PUTPHONEMAXCOUNT){
                List<Object> list = new ArrayList<Object>();
                if(mPutCount < mTotalCount -1){
                    list.addAll(mStorePhoteList.subList(PUTPHONEMAXCOUNT * mPutCount, (mPutCount + 1) * PUTPHONEMAXCOUNT));
                }else{
                    list.addAll(mStorePhoteList.subList(PUTPHONEMAXCOUNT * mPutCount, mStorePhoteList.size()));
                }
                object = new Object[]{ Util.mVerifyUserResult.id, Util.mVerifyUserResult.mUUID, list};
            }else{
                object = new Object[]{ Util.mVerifyUserResult.id, Util.mVerifyUserResult.mUUID, mStorePhoteList};
            }
        }
        if(Actions.DEBUG){
            Log.e("zhu","putDatingTaskActualPhoto="+object);
        }
        if(object == null){
            mHandler.sendEmptyMessage(SyncView.SYNC_7);
            putCustomerTemp();
            return;
        }
        PostMethod.putDatingTaskActualPhoto(object, new XMLRPCMethodCallback() {
            public void callFinished(int status, Object result) {
                boolean isSuccess = ParseData.parsePostData(result);
                if(isSuccess){
                    mPutCount++;
                    if(mPutCount >= mTotalCount){
                        retryCount = 0;
                        mHandler.sendEmptyMessage(SyncView.SYNC_7);
                        putCustomerTemp();
                    }else{
                        putDatingTaskActualPhoto();
                    }
                }else{
                    syncFail();
                }
            }
        }, SalesApplication.mApplicaton.getXMLRPCClient());
    }

    /**
     * putCustomerTemp
     */
    private void putCustomerTemp(){
    	mConnectStep = SETUP_PUTNEWADDEDSTORES;
    	Object[] object = PostData.getNewAddStores(Util.mVerifyUserResult.id, Util.mVerifyUserResult.mUUID, mContext, mStorePropertyList);
    	if(Actions.DEBUG){
    		Log.e("zhu","putCustomerTemp="+object);
    	}
    	if(object == null){
    	    putStoreProrery();
    		return;
    	}
    	PostMethod.postNewAddedStores(object, new XMLRPCMethodCallback() {
			public void callFinished(int status, Object result) {
				boolean isSuccess = ParseData.parsePostData(result);
				if(isSuccess){
					retryCount = 0;
					putStoreProrery();
				}else{
					syncFail();
				}
			}
		}, SalesApplication.mApplicaton.getXMLRPCClient());
    }

    /**
     * putStoreProrery
     */
    private void putStoreProrery(){
        mConnectStep = SETUP_PUTSTOREPROPERTY;
        if(Actions.DEBUG){
            Log.e("zhu","putStoreProrery");
        }
        if(mStorePropertyList.size() > 0){
            PostMethod.postStoreProperty(new Object[]{ Util.mVerifyUserResult.id, Util.mVerifyUserResult.mUUID, mStorePropertyList }, new XMLRPCMethodCallback() {
                public void callFinished(int status, Object result) {
                    boolean isSuccess = ParseData.parsePostData(result);
                    if(isSuccess){
                        retryCount = 0;
                        putCompleted();
                    }else{
                        syncFail();
                    }
                }
            }, SalesApplication.mApplicaton.getXMLRPCClient());
        }else{
            putCompleted();
        }
    }

    /**
     * putCompleted
     */
    private void putCompleted(){
    	mConnectStep = SETUP_PUTCOMPELE;
    	if(Actions.DEBUG){
    		Log.e("zhu","putCompleted");
    	}
    	Object[] object = new Object[]{ Util.mVerifyUserResult.id, Util.mVerifyUserResult.mUUID, new Date(Util.mVerifyUserResult.mStartSyncTime)};
    	PostMethod.postCompeleted(object, new XMLRPCMethodCallback() {
			public void callFinished(int status, Object result) {
				boolean isSuccess = ParseData.parsePostData(result);
				if(isSuccess){
					retryCount = 0;
					syncSuccessEnd();
				}else{
					syncFail();
				}
			}
		}, SalesApplication.mApplicaton.getXMLRPCClient());
    }

    private void requestNetworkTime(){
        if(Actions.DEBUG){
            Log.e("zhu","requestNetworkTime");
        }
        mNetworkTime = 0;
        PostMethod.requestNetworkTime(Util.mVerifyUserResult.id, Util.mVerifyUserResult.mUUID, new XMLRPCMethodCallback() {
            public void callFinished(int status, Object result) {
                mNetworkTime  = ParseData.parseNetworkTime(result);
                getProductCategory();
            }
        },SalesApplication.mApplicaton.getXMLRPCClient());
    }

    /**
     * 同步完成
     */
    private void syncSuccessEnd(){
    	if(Actions.DEBUG){
    		Log.e("zhu","syncSuccessEnd");
    	}
    	deletePutData();
    	saveSyncData();
    	mConnectStep = -1;
    	retryCount = 0;
    	Util.isFirst = false;
    	mHandler.sendEmptyMessage(SyncView.SYNC_SUCCESSEND);
    }

    /**
     * 同步失败
     */
    private void syncFail(){
    	if(Actions.DEBUG){
    		Log.e("zhu","syncFail="+mConnectStep);
    	}
    	if(retryCount < 3){
    		retryCount++;
    		retryConnect();
    	}else{
    		retryCount = 0;
    		mConnectStep = -1;
    		mHandler.sendEmptyMessage(SyncView.SYNC_FAILEDEND);
    	}
    }

    /**
     * 保存同步数据
     */
    private void saveSyncData(){
    	SyncUtils.saveProductCategoryList(mContext, mPClist);
    	SyncUtils.saveProductList(mContext, mPlist);
    	SyncUtils.saveIntegrationStandardList(mContext, mISlist);
		String deletecwhere = SyncUtils.saveCustomerList(mContext, mCustomerList);
    	SyncUtils.saveCustomerShelfsList(mContext, mCSlist);
    	if(deletecwhere.length() > 1){
			mContext.getContentResolver().delete(SalesSettings.ProductShelfsColumns.CONTENT_URI_NO_NOTIFICATION, deletecwhere, null);
		}
    	SyncUtils.saveCRCinfoList(mContext, mCRClist);
    	SyncUtils.savePusherStoreList(mContext, mPusherList);
    	SyncUtils.saveTaskList(mContext, mTaskList, mNetworkTime);
    	SyncUtils.saveRemindMsgList(mContext, mRemindList);
    	SyncUtils.saveStorePropertyList(mContext, mStorePropertyList);
    	mCustomerShelfData.updateSCImsg();
    }

    /**
     * 删除上传数据
     */
    public void deletePutData(){
        PostData.deleteStorePhotos(mContext);
        PostData.deleteCustomers(mContext);
        PostData.deleteCustomersShelf(mContext);
        PostData.deletePusherInfo(mContext);
        PostData.deleteAllNewAddedStores(mContext);
        PostData.deleteAllRemindsData(mContext);
        mCustomerShelfData.delteSqData();
    }

    public void detory(){
    	resetData();
    	if(mCustomerShelfData != null){
    		mCustomerShelfData.destory();
    		mCustomerShelfData = null;
    	}
    }
}
